#!/usr/bin/env python
#
# tweet.py
#
# Contains the Tweet class

import sys
sys.path.append('depends')

import pygame
import urllib
import os, random
from copy import copy
from pygame.locals import *
from TextWrap import *

class Tweet(pygame.sprite.Sprite):
	"""
	Tweet class holds individual tweets and need to be added to
	conversations to be viewed.
	"""
	# Static image variable so it is only loaded once
	image = None
	treeImgList = []
	messageImg = None
	urlImages = {}
	def __init__(self, message, id=None, username=None,imgUrl = "http://s3.amazonaws.com/twitter_production/profile_images/146520371/avatarpic-l.png"):
		"""
		Initilize a Tweet with a message.  WILL NEED MORE INFO EVENTUALLY
		"""
		self.message = message
		if Tweet.image == None:
			Tweet.image, self.rect = load_image("birdy.gif", -1)
			Tweet.treeImage, self.treeRect = self.loadTrees()
			Tweet.messageImg, self.messageRect = load_image("comments.gif", None)
		else:
			self.image = Tweet.image
			self.rect = Tweet.image.get_rect()

		# Check url image dictionary for image
		if Tweet.urlImages.has_key(imgUrl):
			self.urlImg = Tweet.urlImages[imgUrl]
		else:
			# Load the image
			self.urlImg, urlRect = load_webimage(imgUrl)
			# add it to the dictionary
			Tweet.urlImages[imgUrl] = self.urlImg
		self.id = id
		self.username = username
		self.treeIndex = 0
		# Start the image as transparent
		#self.image.set_alpha(0)
		self.screen = pygame.display.get_surface()
		self.x ,self.y = 0,0
		self.alpha = 0
		self.theta = 0
		self.radius = 0
		self.rect.topleft = (self.x, self.y)
		self.currTicks = pygame.time.get_ticks()

	def loadTrees(self):
		tree0, tmpRect = load_image("tree1.gif", -1)
		tree1, tmpRect = load_image("tree2.gif", -1)
		tree2, tmpRect = load_image("tree4.gif", -1)
		Tweet.treeImgList = [tree0, tree1, tree2]

		return Tweet.treeImgList[0], Tweet.treeImgList[0].get_rect()

	def increaseTree(self):
		if self.treeIndex < len(Tweet.treeImgList) - 1:
			self.treeIndex += 1
			currCenter = self.rect.center
			self.image = Tweet.treeImgList[self.treeIndex]
			self.rect = self.image.get_rect()
			self.rect.center = currCenter
		
	def update(self):
		"""
		Update Tweet before drawing to the screen
		"""

	def draw(self):
		self.screen.blit(self.image, self.rect.topleft)

	def getSurface(self):
		tweetSur = copy(Tweet.messageImg)
		#sizeImg = pygame.transform.smoothscale(self.urlImg, (100, 100))
		# Blit the urlImage in the center of the square of the comment image
		centerRect = self.urlImg.get_rect()
		centerRect.center = (245, 60)		
		tweetSur.blit(self.urlImg, centerRect)
		font=pygame.font.Font("fonts/DoradoHeadline.ttf", 15)
		lineList = wrapline(self.message, font, 500)
		lineSurList = []
		for line in lineList:
			lineSurList.append(font.render(line, True, (0,0,0)))

		yPos = 12
		for line in lineSurList:
			tweetSur.blit(line, (334, yPos))
			yPos += line.get_height()

		return tweetSur 
		
		

	def changeImage(self):
		topLeft = self.rect.topleft
		self.image = Tweet.treeImage
		self.rect = self.image.get_rect()
		self.rect.topleft = topLeft


def load_image(name, colorkey=None):
	fullname= os.path.join('images', name)
	#imgFile, headers = urllib.urlretrieve('http://pixelbud.com/images/twitter_bird_small.gif')
	#print imgFile
	try:
		image = pygame.image.load(fullname)
		#image = pygame.image.load(imgFile)
	except pygame.error, message:
		print 'Cannot load image:', fullname
		#print 'Cannot load image:', message
		raise SystemExit, message
	image = image.convert()
	if colorkey is not None:
		if colorkey is -1:
			colorkey = image.get_at((0,0))
			image.set_colorkey(colorkey, RLEACCEL)
	return image, image.get_rect()

def load_webimage(url, colorkey = None):
	imgFile, headers = urllib.urlretrieve(url)
	try:
		image = pygame.image.load(imgFile)
	except pygame.error, message:
		#print url
		#print 'Cannot load image:', message
		#raise SystemExit, message
		image = Tweet.image
	image = image.convert()
	# Resize the image to fit the screen properly
	image = resizeWebImage(image)
	if colorkey is not None:
		if colorkey is -1:
			colorkey = image.get_at((0,0))
			image.set_colorkey(colorkey,RLEACCEL)
	return image, image.get_rect()

def resizeWebImage(image):
	backWidth = 90
	backHeight = 92
	imageWidth = image.get_width()
	imageHeight = image.get_height()

	if imageWidth >= imageHeight:
		# Resize image based on width
		if imageWidth > backWidth:
			diff = imageWidth - backWidth
			finWidth = imageWidth - diff
			finHeight = imageHeight - diff
		else:
			diff = backWidth - imageWidth
			finWidth = imageWidth + diff
			finHeight = imageWidth + diff
	else:
		# Resize image based on height
		if imageHeight > backHeight:
			diff = imageHeight - backHeight
			finHeight = imageHeight - diff
			finWidth = imageWidth - diff
		else:
			diff = backHeight - imageHeight
			finHeight = imageHeight + diff
			finWidth = imageWidth + diff
	# Resize image to the finHeight and finWidth
	return pygame.transform.scale(image, (finWidth, finHeight))

if __name__ == "__main__":
	newTweet = Tweet("hellO")
